package view;

import com.alee.extended.panel.WebButtonGroup;
import com.alee.laf.button.WebToggleButton;
import com.alee.laf.label.WebLabel;
import com.alee.laf.toolbar.WebToolBar;
import com.alee.managers.tooltip.TooltipManager;
import com.alee.managers.tooltip.TooltipWay;
import controller.App;
import util.FontLoader;
import util.ImageLoader;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Toolbar extends WebToolBar {
    private static final long serialVersionUID = -5261480899581379323L;
    public WebToggleButton homeButton, friendsButton, statsButton, searchButton;

    /**
     * Application toolbar.
     */
    public Toolbar() {
        // set toolbar style
        this.setUndecorated(true);
        this.setFloatable(false);
        this.setPreferredSize(new Dimension(640, 50));
        this.setOpaque(true);
        this.setBackground(new Color(80, 58, 150));

        // create logo
        WebLabel label = new WebLabel("uw");
        WebLabel label2 = new WebLabel("connect");
        label.setMargin(0, 16, 8, 2);
        label2.setMargin(0, 2, 8, 8);
        label.setForeground(Color.white);
        label2.setForeground(Color.white);
        Font osBold = FontLoader.getFont("OpenSans-Bold.ttf");
        Font osLight = FontLoader.getFont("OpenSans-Light.ttf");
        label.setFont(osBold.deriveFont(40f));
        label2.setFont(osLight.deriveFont(40f));
        this.add(label);
        this.add(label2);

        // create three buttons - home initially selected.
        homeButton = new WebToggleButton(ImageLoader.getImage("home-white.png"));
        homeButton.setUndecorated(true);
        homeButton.setMargin(6);
        TooltipManager.addTooltip(homeButton, "Home Feed", TooltipWay.down, 150);

        friendsButton = new WebToggleButton(ImageLoader.getImage("users.png"));
        friendsButton.setUndecorated(true);
        friendsButton.setMargin(6);
        TooltipManager.addTooltip(friendsButton, "Friends", TooltipWay.down, 150);

        statsButton = new WebToggleButton(ImageLoader.getImage("stats.png"));
        statsButton.setMargin(6, 6, 6, 8);
        statsButton.setUndecorated(true);
        TooltipManager.addTooltip(statsButton, "Statistics", TooltipWay.down, 150);

        searchButton = new WebToggleButton(ImageLoader.getImage("search.png"));
        searchButton.setMargin(6, 6, 6, 8);
        searchButton.setUndecorated(true);
        TooltipManager.addTooltip(searchButton, "Search", TooltipWay.down, 150);

        // create an action listener for all 4 buttons
        ActionListener a = new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                handleButtonStateChange();
            }
        };

        statsButton.addActionListener(a);
        friendsButton.addActionListener(a);
        homeButton.addActionListener(a);
        searchButton.addActionListener(a);

        // add buttons to a group to facilitate single selection
        WebButtonGroup iconsGroup = new WebButtonGroup(true, homeButton, friendsButton, statsButton, searchButton);
        iconsGroup.setButtonsDrawFocus(false);
        this.addToEnd(iconsGroup);

        // add user name
        WebLabel wl = new WebLabel(App.getCurrentUser().name);
        wl.setFont(osBold.deriveFont(16f));
        wl.setForeground(Color.white);
        wl.setMargin(8, 8, 8, 4);
        this.addToEnd(wl);

        // add user icon
        WebLabel wi = new WebLabel(ImageLoader.getImage("kanye.png"));
        wi.setMargin(8, 4, 8, 16);
        this.addToEnd(wi);
    }

    /**
     * Handle a nav button click. Sets the proper nav icon.
     */
     public void handleButtonStateChange() {
        if (statsButton.isSelected()) {
            statsButton.setIcon(ImageLoader.getImage("stats-white.png"));
        } else {
            statsButton.setIcon(ImageLoader.getImage("stats.png"));
        }

        if (friendsButton.isSelected()) {
            friendsButton.setIcon(ImageLoader.getImage("users-white.png"));
        } else {
            friendsButton.setIcon(ImageLoader.getImage("users.png"));
        }

        if (homeButton.isSelected()) {
            homeButton.setIcon(ImageLoader.getImage("home-white.png"));
        } else {
            homeButton.setIcon(ImageLoader.getImage("home.png"));
        }

         if (searchButton.isSelected()) {
             searchButton.setIcon(ImageLoader.getImage("search-white.png"));
         } else {
             searchButton.setIcon(ImageLoader.getImage("search.png"));
         }
    }

}
